<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="rolePage">
        <el-row>
          <el-col :sm="4">
            <div class="g-title">碎片率</div>
          </el-col>
          <el-col :sm="20" style="text-align: right">
            <span class="role-add" @click="handleCreate()"><i class="el-icon-plus" />添加</span>
          </el-col>
        </el-row>

        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          @change="fetchData()"
        />
      </div>
    </div>
    <el-row class="table">
      <el-table
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="shardsDate" label="日期" show-overflow-tooltip align="center" />
        <el-table-column
          prop="originalLossGoal"
          label="原损目标"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="workLossGoal"
          label="工损目标"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="originalLoss1"
          label="原损L1"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="workLoss1"
          label="工损L1"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="originalLoss2"
          label="原损L2"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="workLoss2"
          label="工损L2"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="originalLoss3"
          label="原损L3"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="workLoss3"
          label="工损L3"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="originalLoss4"
          label="原损L4"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="workLoss4"
          label="工损L4"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button
              v-if="row.status != 'deleted'"
              size="mini"
              type="danger"
              @click="handleDelete(row, $index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          />
        </el-col>
      </el-row>
    </el-row>
    <!-- 查看详情弹窗 -->
    <el-dialog title="查看" :visible.sync="dialogSee">
      <el-form
        ref="SeeForm"
        :model="SeeData"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="投入达成">
          <span>{{ SeeData.inputFinish }}</span>
        </el-form-item>
        <el-form-item label="碎片">
          <span>{{ SeeData.remarkSuiPian }}</span>
        </el-form-item>
        <el-form-item label="良率">
          <span>{{ SeeData.remarkGoodRates }}</span>
        </el-form-item>
        <el-form-item label="其他">
          <span>{{ SeeData.remarkOther }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSee = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加修改弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="日期" prop="shardsDate">
          <el-date-picker
            v-model="temp.shardsDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="原损目标" prop="originalLossGoal">
          <el-input v-model="temp.originalLossGoal" placeholder="请输入内容" clearable />
        </el-form-item>
        <el-form-item label="工损目标" prop="workLossGoal">
          <el-input v-model="temp.workLossGoal" placeholder="请输入内容" clearable />
        </el-form-item>
        <el-form-item label="原损L1" prop="originalLoss1">
          <el-input v-model="temp.originalLoss1" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="工损L1" prop="workLoss1">
          <el-input v-model="temp.workLoss1" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="原损L2" prop="originalLoss2">
          <el-input v-model="temp.originalLoss2" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="工损L2" prop="workLoss2">
          <el-input v-model="temp.workLoss2" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="原损L3" prop="originalLoss3">
          <el-input v-model="temp.originalLoss3" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="工损L3" prop="workLoss3">
          <el-input v-model="temp.workLoss3" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="原损L4" prop="originalLoss4">
          <el-input v-model="temp.originalLoss4" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="工损L4" prop="workLoss4">
          <el-input v-model="temp.workLoss4" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          {{ textMap[dialogStatus] }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import './css/index.scss'
import { shardsAdd, shardsAllItems, shardsUpdate, shardsDelete } from '@/api/report'
export default {
  data: function() {
    return {
      activeCardName: 'all',
      dialogFormVisible: false,
      dialogSee: false,
      dialogStatus: '',

      dateRange: null,
      langtopicTypeData: [],
      textMap: {
        update: '修改',
        create: '添加'
      },
      SeeData: {
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      },
      temp: {
        shardsDate: '',
        originalLossGoal: '',
        workLossGoal: '',
        originalLoss1: '',
        workLoss1: '',
        originalLoss2: '',
        workLoss2: '',
        originalLoss3: '',
        workLoss3: '',
        originalLoss4: '',
        workLoss4: ''
      },
      rules: {
        shardsDate: [{ required: true, message: '日期没有选择', trigger: 'change' }],
        originalLossGoal: [{ required: true, message: '原损目标不能为空', trigger: 'blur' }],
        workLossGoal: [{ required: true, message: '工损目标不能为空', trigger: 'blur' }],
        originalLoss1: [{ required: true, message: '原损L1不能为空', trigger: 'blur' }],
        workLoss1: [{ required: true, message: '工损L1不能为空', trigger: 'blur' }],
        originalLoss2: [{ required: true, message: '原损L2不能为空', trigger: 'blur' }],
        workLoss2: [{ required: true, message: '工损L2不能为空', trigger: 'blur' }],
        originalLoss3: [{ required: true, message: '原损L3不能为空', trigger: 'blur' }],
        workLoss3: [{ required: true, message: '工损L3不能为空', trigger: 'blur' }],
        originalLoss4: [{ required: true, message: '原损L4不能为空', trigger: 'blur' }],
        workLoss4: [{ required: true, message: '工损L4不能为空', trigger: 'blur' }]

      },
      total: 0,
      pageNo: 1,
      pageSize: 10,
      list: [], // 全部数据
      listDataSelected: [] // 选中数据
    }
  },
  created() {
    // 获取数据
    this.fetchData()
  },
  methods: {
    // 重置数据
    resetTemp() {
      this.temp = {
        shardsDate: '',
        originalLossGoal: '',
        workLossGoal: '',
        originalLoss1: '',
        workLoss1: '',
        originalLoss2: '',
        workLoss2: '',
        originalLoss3: '',
        workLoss3: '',
        originalLoss4: '',
        workLoss4: ''
      }
    },
    // 查询所有数据
    async SeeDataQueryAllItems() {
      const params = {
        pageNum: 1,
        pageSize: 100
      }
      await queryAllItems(params).then(res => {
        this.langtopicTypeData = res.data.content
      })
    },
    // 计算计划达成率
    handleInputChange() {
      console.log('temp', this.temp)
      if (this.temp.plannedInputs && this.temp.actualInputs) {
        this.temp.plannedAchiRates = ((this.temp.actualInputs / this.temp.plannedInputs) * 100).toFixed(2) + '%'
        this.$forceUpdate()
      }
    },
    // 打开添加早会弹窗
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //  修改早会
    handleUpdate(row) {
      const data = Object.assign({}, row)
      this.temp = {
        shardsDate: data.shardsDate,
        originalLossGoal: data.originalLossGoal,
        workLossGoal: data.workLossGoal,
        originalLoss1: data.originalLoss1,
        workLoss1: data.workLoss1,
        originalLoss2: data.originalLoss2,
        workLoss2: data.workLoss2,
        originalLoss3: data.originalLoss3,
        workLoss3: data.workLoss3,
        originalLoss4: data.originalLoss4,
        workLoss4: data.workLoss4,
        shardsId: data.shardsId
      }
      console.log('temp', this.temp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加早会提交数据
    createData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const data = {
            shardsDate: this.temp.shardsDate,
            originalLossGoal: this.temp.originalLossGoal,
            workLossGoal: this.temp.workLossGoal,
            originalLoss1: this.temp.originalLoss1,
            workLoss1: this.temp.workLoss1,
            originalLoss2: this.temp.originalLoss2,
            workLoss2: this.temp.workLoss2,
            originalLoss3: this.temp.originalLoss3,
            workLoss3: this.temp.workLoss3,
            originalLoss4: this.temp.originalLoss4,
            workLoss4: this.temp.workLoss4
          }
          console.log('data', data)
          await shardsAdd(data).then((r) => {
            console.log('temp', this.temp)
            this.dialogFormVisible = false
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 修改早会提交数据
    updateData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const date = this.getTime(this.temp.shardsDate)

          const data = {
            shardsDate: date,
            originalLossGoal: this.temp.originalLossGoal,
            workLossGoal: this.temp.workLossGoal,
            originalLoss1: this.temp.originalLoss1,
            workLoss1: this.temp.workLoss1,
            originalLoss2: this.temp.originalLoss2,
            workLoss2: this.temp.workLoss2,
            originalLoss3: this.temp.originalLoss3,
            workLoss3: this.temp.workLoss3,
            originalLoss4: this.temp.originalLoss4,
            workLoss4: this.temp.workLoss4,
            shardsId: this.temp.shardsId
          }
          console.log('data', data)
          await shardsUpdate(data).then((r) => {
            this.dialogFormVisible = false
            this.singleTime = this.temp.singleTime
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 删除早会
    async handleDelete(row) {
      await shardsDelete({ id: row.shardsId }).then((r) => {
        if (r && r.code == 200) {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: r.message
          })
        }
      })
    },
    //  查看账户
    SeeRow(data) {
      this.SeeData = {
        inputFinish: data.inputFinish,
        remarkSuiPian: data.remarkSuiPian,
        remarkGoodRates: data.remarkGoodRates,
        remarkOther: data.remarkOther
      }
      this.dialogSee = true
    },
    // 分页组件
    pageSizeChange(newSize) {
      this.pageSize = newSize
      this.pageNo = 1
      this.fetchData()
    },

    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo
      }
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,

        starTime: '',
        endTime: ''
      }
      if (this.dateRange) {
        const start = this.getTime(this.dateRange[0])
        const end = this.getTime(this.dateRange[1])
        params.starTime = start
        params.endTime = end
      }
      const data = await shardsAllItems(params)
      this.list = data.data.content
      this.total = data.data.totalSize
    },
    // 表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0)
      val.forEach(e => {
        this.listDataSelected.push(e)
      })
    },
    getTime(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      return y + '-' + m + '-' + d
    }
  }
}
</script>
<style lang="scss" scoped>
.userlist {
    margin-right: 10px;
}

.title {
    height: 20px;
    padding-left: 5px;
    line-height: 20px;
    font-size: 20px;
    font-weight: bold;
    border-left: 7px solid #1587F9;
    margin-bottom: 23px;
    display: inline-block;
}
</style>
